# Copyright (c) 2025, hzmes and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document


class HSFSupplierEvaluate(Document):
	# 校验是否为空值
	def before_validate(self):
		if self.item and len(self.item) == 1:
			if not self.item[0].batch_pass_rate: self.item[0].batch_pass_rate=0
			if not self.item[0].process_anomalies: self.item[0].process_anomalies=0
			if not self.item[0].customer_complaints: self.item[0].customer_complaints=0
			if not self.item[0].environmental_accidents: self.item[0].environmental_accidents=0
			if not self.item[0].provision_of_materials: self.item[0].provision_of_materials=0
			if not self.item[0].timeliness_of_handling: self.item[0].timeliness_of_handling=0
			if not self.item[0].sqe_cooperation_level: self.item[0].sqe_cooperation_level=0
			if not self.item[0].sqe_others: self.item[0].sqe_others=0
			if not self.item[0].product_price: self.item[0].product_price=0
			if not self.item[0].price_reduction: self.item[0].price_reduction=0
			if not self.item[0].open_cost: self.item[0].open_cost=0
			if not self.item[0].new_product_quotation: self.item[0].new_product_quotation=0
			if not self.item[0].payment_cycle: self.item[0].payment_cycle=0
			if not self.item[0].delayed_delivery: self.item[0].delayed_delivery=0
			if not self.item[0].order_response_rate: self.item[0].order_response_rate=0
			if not self.item[0].purchase_cooperation_level: self.item[0].purchase_cooperation_level=0
			if not self.item[0].new_product_cooperation_level: self.item[0].new_product_cooperation_level=0
			if not self.item[0].making_efficiency: self.item[0].making_efficiency=0
			if not self.item[0].innovation_capability: self.item[0].innovation_capability=0

	def before_save(self):
		if self.item and len(self.item) == 1:
			quality_score = self.item[0].batch_pass_rate + self.item[0].process_anomalies + self.item[0].customer_complaints + self.item[0].environmental_accidents + self.item[0].provision_of_materials + self.item[0].timeliness_of_handling + self.item[0].sqe_cooperation_level + self.item[0].sqe_others
			purchase_score = self.item[0].product_price + self.item[0].price_reduction + self.item[0].open_cost + self.item[0].new_product_quotation + self.item[0].payment_cycle + self.item[0].delayed_delivery + self.item[0].order_response_rate + self.item[0].purchase_cooperation_level
			rd_score = self.item[0].new_product_cooperation_level + self.item[0].making_efficiency + self.item[0].innovation_capability
			total_score = quality_score * 0.6 + purchase_score * 0.2 + rd_score * 0.2
			self.quality_score = quality_score * 0.6
			self.purchase_score = purchase_score * 0.2
			self.rd_score = rd_score * 0.2
			self.total_score = total_score
			if total_score >= 90:
				self.level = 'A'
			if total_score >= 80 and total_score < 90:
				self.level = 'B'
			if total_score >= 60 and total_score < 80:
				self.level = 'C'
			if total_score < 60:
				self.level = 'D'
		elif len(self.item) > 1:
			frappe.throw('保存校验规则: 有且只有一个分录行!')
		elif not self.item:
			self.quality_score = 0
			self.purchase_score = 0
			self.rd_score = 0
			self.total_score = 0
			self.level = ''
